#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;

class PthreadData
{
    public:
    PthreadData(int num,time_t time,const string& name)
    :_num(num),
    _time((uint64_t)time),
    _name(name)
    {}
    public: 
    int _num;
    uint64_t _time;
    string _name;
};

class COUNT

{
    public:
    COUNT(int sum)
    :_sum(sum)
    {}

    int _sum;
};



/*void *pthreadrun(void *arg)
{
    char *name = (char *)arg;
    while (1)
    {
        cout << name << "，我在运行。。。" << endl;
        sleep(5);
        break;
    }
    delete name;
}*/



void* pthreadrun(void* arg)
{
    PthreadData* pd=(PthreadData*)arg;
    cout<<"name:"<<pd->_name<<" "<<"num:"<<pd->_num<<" "<<"time:"<<pd->_time<<endl;
    sleep(5);
    int sum=0;
    for(int i=0;i<=100+pd->_num;i++)
    {
        sum+=i;
    }
    COUNT* ptr=new COUNT(sum);
    delete pd;
    return ptr;
}
int main()
{
    pthread_t t[10];
    for (int i = 0; i < 10; i++)
    {
        char arg[64];
        sprintf(arg, "pthread-%d", i + 1);
        PthreadData* pd=new PthreadData(i+1,time(nullptr),arg);
        pthread_create(t + i, nullptr, pthreadrun, pd);
        sleep(1);
    }
    void* ret=nullptr;
    for (int i = 0; i < 10; i++)
    {
        pthread_join(t[i],&ret);
        cout<<"pthread->"<<i+1<<":"<<(((COUNT*)ret)->_sum)<<endl;
        delete (COUNT*)ret;
        cout << i + 1 << "回收完成" << endl;
    }
    cout << "pthread全部回收完成" << endl;
    /*while(1)
    {
        sleep(1);
        cout<<"我是main线程，我在运行中。。。";
    }*/
    return 0;
}